/*smog_mechanisms.do: This script produces tables showing the mechanism behind
re-inspections at low-quality stations having no effect on air quality*/

clear all

capture log close
set more off

cd "/home/work/projects/smog_check/Smog Check Data"

use quarter county station_id svfr_ratio fpr_current using asmair_star

duplicates drop
tempfile temp
save `temp'

//local debug if substr(vin,-3,2)=="11"

use fpr_precollapse `debug'

joinby station_id quarter using `temp'

sort vin date teststart


/* Identify inspection cycles with an index*/

by vin: gen cycle = sum(initial_inspection)


sort vin cycle date teststart

/* Only interested in failing cycles where we observe a re-inspection
(whether or not this was a pass--cars that fail and vanish aren't relevant)*/

by vin cycle: gen int cycle_n = _n

by vin cycle: gen int cycle_checks = _N

/* Calculate the station-level correlation between the share of inspected vehicles
that are re-inspected, and our quality metrics (to report in the text*/

preserve

gen re_inspection = cycle_n > 1

gen re_inspection_move = cycle_n >1 & station_id > station_id[_n-1]

collapse re_inspection* fpr_current svfr_ratio, by(quarter station_id county)

corr re_inspection* fpr_current

restore

by vin cycle: keep if cycle_checks >1 & overall[1]!="P"

by vin cycle: gen byte same_station = station_id[1] == station_id[_N]

by vin cycle: gen byte gross_sum = sum(overall=="G")

by vin cycle: gen final_within_week = date[_N] - date[1] <=7


/* Want to know how often consumers switch, how many they get re-inspected,
and how the follow up station compares to the original station*/

by vin cycle: gen change_cfpr = fpr_current[_N] - fpr_current[1] if same_station == 0


eststo check_all: estpost tabstat final_within_week same_station change_* if cycle_checks==cycle_n & gross_sum ==0 , col(stat) 
eststo check2: estpost tabstat final_within_week same_station change_* if cycle_checks==cycle_n & gross_sum ==0 &cycle_checks == 2 , col(stat)
eststo check3: estpost tabstat final_within_week same_station change_* if cycle_checks==cycle_n & gross_sum ==0 &cycle_checks == 3 , col(stat)
eststo check4: estpost tabstat final_within_week same_station change_* if cycle_checks==cycle_n & gross_sum ==0 &cycle_checks == 4 , col(stat)
eststo check5plus: estpost tabstat final_within_week same_station change_* if cycle_checks==cycle_n & gross_sum ==0 &cycle_checks >= 5 , col(stat)


#delimit ;
esttab check* using mechanism_switchers.tex, main(mean) not booktabs replace
title(Consumers' Shopping Behavior After a Failed Initial Inspection\label{tab:mechanism-switcher})
varlabels(final_within_week "Final Inspection Within 1 week" same_station "Final Inspection At Same Station" change_svfr "Switchers' Change in SVFR" 
change_cfpr "Switchers' Change in C-FPR")
mgroup(" " "Total Number of Inspections in Cycle", pattern(1 1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}))
mtitles(All 2 3 4 5+) nonum nonote
addnote("Statistics are means. A unit of observation is an inspection cycle, limited to cycles with more than one inspection, with no 
\`\`gross'' failures. 
\`\`Final Inspection at Same Station'' is equal to 1 if the first and last inspection in the cycle are at the same station.
\`\`Switchers' change in C-FPR'' is the difference between the C-FPR score of the first and last station in the inspection cycle.")
substitute(\end{tabular} \end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents  
	\begin{tabular}	\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}
	{l}{\footnotesize {p{\linewidth}}{\footnotesize)

;
#delimit cr 
instanttex using mechanism.tex, replace table(mechanism_switchers.tex) p(calc)




